﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeTalk
{
    public static class TextUtilz
    {
        public static string CapFirstLetter(string t)
        {
            if (t.Length == 1)
            {
                return t.ToUpperInvariant();
            }
            else if (t.Length > 1)
            {
                return t.Substring(0, 1).ToUpperInvariant() + t.Substring(1);
            }

            return t;
        }

        public static string UncapFirstLetter(string t)
        {
            if (t.Length == 1)
            {
                return t.ToLowerInvariant();
            }
            else if (t.Length > 1)
            {
                return t.Substring(0, 1).ToLowerInvariant() + t.Substring(1);
            }

            return t;
        }

        public static string MakeVariabelName(string[] words, SymbolCategory category)
        {
            string s = words[0];
            for (int i = 1; i < words.Length; i++)
            {
                s += CapFirstLetter(words[i]);
            }

            if (category == SymbolCategory.Local)
                return UncapFirstLetter(s);
            
            if (category == SymbolCategory.Hidden)
                return "_" + CapFirstLetter(s);

            return CapFirstLetter(s);
        }

        public static string MakeVariabelName(string text)
        {
            return MakeVariabelName(text.Split(new char[] { ' ' }), SymbolCategory.Open);
        }

        public static string MakeVariabelName(string text, SymbolCategory category)
        {
            return MakeVariabelName(text.Split(new char[] { ' ' }), category);
        }

        public static string PieceTogetherWithSpaces(string[] words)
        {
            if (words.Length < 1)
            {
                return "";
            }
            else if (words.Length == 1)
            {
                return words[0];
            }
            else
            {
                string s = words[0];
                for (int i = 1; i < words.Length; i++)
                {
                    s += " " + words[i];
                }

                return s;
            }
        }

        public static string MakePronounceable(string symbol)
        {
            StringBuilder currentWord = new StringBuilder();
            StringBuilder result = new StringBuilder();
            
            if (char.IsLetterOrDigit(symbol[0]))
                currentWord.Append(symbol[0]);

            for (int i = 1; i < symbol.Length; i++)
            {
                char c = symbol[i];

                if (!char.IsDigit(c) && !char.IsUpper(c))
                {
                    currentWord.Append(c);
                }
                else
                {
                    if (result.Length > 0)
                        result.Append(' ');

                    result.Append(currentWord.ToString());

                    currentWord.Remove(0, currentWord.Length);
                    currentWord.Append(c);
                }
            }

            if (result.Length > 0)
                result.Append(' ');

            result.Append(currentWord.ToString());

            return result.ToString();
        }

        public static string GetUndoString(string txt)
        {
            if (txt.Length < 1) return "";

            int nSpaces = txt.Split(' ', '\n').Length;
            string s = "^z";
            for (int i = 1; i < nSpaces; i++)
                s += "^z";
            return s;
        }
    }
}
